package com.simtop.car.controller;


import com.github.pagehelper.PageInfo;
import com.simtop.car.common.ServerResponse;
import com.simtop.car.pojo.*;
import com.simtop.car.service.UserService;
import com.simtop.car.utils.EmptyUtil;
import com.simtop.car.utils.ExcelUtils;
import com.simtop.car.utils.JwtUtil;
import com.simtop.car.utils.RedisUtil;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
@CrossOrigin //todo 跨域问题
public class UserController {

    private static Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    public UserService userService;


    @Autowired
    public RedisUtil redisUtil;


    /**
     * 获取角色身份
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getRole",method = RequestMethod.GET)
    public ServerResponse<List<Role>> getRole(){
        List<Role> roleList=userService.getRole();
        return ServerResponse.createBySuccess(roleList);
    }


    /**
     * 用户登录（返回类）
     * @param user（loginName，password，roleId）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ServerResponse<String> login(@RequestBody User user){
        User loginUser =userService.login(user);
        if(EmptyUtil.isNotEmpty(loginUser)){
            long time=System.currentTimeMillis();
            loginUser.setLoginTime(time);
            loginUser.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);//置空密码
            // todo important token有效期10个小时
            String token = JwtUtil.sign(loginUser,time+12600*1000);//token有效期3.5小时
            return ServerResponse.createBySuccess(token);
        }else{
            return ServerResponse.createByErrorMsg("账号名或者密码输入错误");
        }
    }
    /**
     * 注册用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register",method =RequestMethod.POST)
    public ServerResponse<String> register (@RequestBody User user){
        logger.info("注册用户"+user);
        return userService.register(user);
    }



    /**
     * 注册时 1、获取后台生成的邮箱验证码
     * 参数需要安全，接口安全
     * @return
     */
    @RequestMapping(value = "/checkCode",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> sendEmailCode(@RequestBody String email){
        JSONObject jsonObj = new JSONObject(email);
        logger.info("email"+jsonObj.getString("email"));
        //根据邮箱获取验证码
        return userService.generateCheckCode(jsonObj.getString("email"));
    }

    /**
     * 忘记密码时获取验证码 邮箱验证码
     *
     */
    @RequestMapping(value = "/forgetCheckCode",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetSendEmailCode(@RequestBody String email){
        JSONObject jsonObj = new JSONObject(email);
        logger.info("email"+jsonObj.getString("email"));
        //根据邮箱获取验证码
        return userService.forgetSendEmailCode(jsonObj.getString("email"));
    }

    /**
     * 忘记密码时根据邮箱地址获取验证码 更新
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updatePassword(@RequestBody User user){
        return userService.updatePassword(user);
    }


    /**
     * 生成成绩报告
     * @param request
     * @param grade
     * @return
     */
    @RequestMapping(value = "/submitGrades",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Grade> submitGrades(HttpServletRequest request,@RequestBody String grade){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(grade);
        String[] brandType=jsonObject.getString("brandType").split(",");
        String[] word=jsonObject.getString("word").split(",");
        Integer sourceCount=jsonObject.getInt("sourceCount");
        String ciYun=jsonObject.getString("word");
        Integer questionNums=jsonObject.getInt("questionNums");
        String answer=jsonObject.getString("answer");
        return  userService.submitGrades(brandType,u,word,sourceCount,ciYun,questionNums,answer);
    }


    @RequestMapping(value = "/a",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Object> submitGrades(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        logger.info("用户解密token{}",u);
        return  ServerResponse.createBySuccess(u);
    }


    /**
     * 提交评论
     * @param request
     * @param evaluate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/submitComments",method = RequestMethod.POST)
    public ServerResponse<String> submitComments(HttpServletRequest request,@RequestBody String evaluate){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(evaluate);
        Integer gradeId=jsonObject.getInt("gradeId");
        String comment=jsonObject.getString("comment");
        return userService.submitComments(gradeId,comment);
    }

    /**
     * 游客登录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "expertLogin",method = RequestMethod.POST)
    public ServerResponse<String> expertLogin(){
        User expertLoginUser = new User();
        expertLoginUser.setRoleId(4);
        expertLoginUser.setUsername("评审账户");
        expertLoginUser.setSchool("华东师范大学");
        long time=System.currentTimeMillis();
        expertLoginUser.setLoginTime(time);
        userService.expertLogin(expertLoginUser);
        expertLoginUser.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);//置空密码
        String token = JwtUtil.sign(expertLoginUser,time+12600*1000);//token有效期3.5小时
        Integer touristNums=0;
        if(redisUtil.hasKey("touristNums")){
            touristNums= (Integer) redisUtil.get("touristNums");
        }
        redisUtil.set("touristNums",++touristNums);
        return ServerResponse.createBySuccess(token);
    }

    /**
     * 获取成绩
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getGrade",method = RequestMethod.POST)
    public ServerResponse<PageInfo<Grade>> getGrade(HttpServletRequest request, @RequestBody String grades){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
//        User u=new User();
//        u.setId(503);
//        u.setRoleId(4);
        JSONObject jsonObject=new JSONObject(grades);
        Integer pageSize=jsonObject.getInt("pageSize");
        Integer pageNum=jsonObject.getInt("pageNum");
        String username= jsonObject.getString("username");
        u.setUsername(username);
        return userService.getGrade(pageSize,pageNum,u);
    }

    /**
     * 首页图标展示
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/getNumbers",method = RequestMethod.POST)
    public ServerResponse<IconShow> getNumbers(){
        Integer userNums=userService.getUserNums();//用户总数量
        Integer experimentNums=userService.getExperimentNums();//实验总次数
        Echarts3 echarts3=userService.selectScoreUser();//用户成绩分布
        List<Echarts2> echarts2List=userService.selectRoleUser();//用户数量统计分析
        Integer nums=0;

        if(redisUtil.hasKey("nums")){
            nums= (Integer) redisUtil.get("nums");
        }
        redisUtil.set("nums",++nums);
        if(redisUtil.hasKey("touristNums")){
            Integer touristNums= (Integer) redisUtil.get("touristNums");
//            userNums+=touristNums;
//            Echarts2 echarts2=new Echarts2("游客",touristNums);
//            echarts2List.add(echarts2);

        }
        IconShow iconShow=new IconShow();
        iconShow.setUserNums(userNums);
        iconShow.setExperimentNums(experimentNums);
        iconShow.setScore(echarts3);
        iconShow.setUserNumsAnalyze(echarts2List);
        iconShow.setNums(nums);
        return ServerResponse.createBySuccess(iconShow);
    }


    /**
     * 主题品牌和竞争品牌的忠诚度
     * @param request
     * @param loyaltyIndex
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getLoyaltyIndex",method = RequestMethod.POST)
    public ServerResponse<List> getLoyaltyIndex(HttpServletRequest request, @RequestBody String loyaltyIndex){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(loyaltyIndex);
        String[] brandType=jsonObject.getString("brandType").split(",");//汽车品牌
        return userService.getLoyaltyIndex(u,brandType);
    }

    /**
     * 获取合格率
     * @param request
     * @param yield
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getYield",method = RequestMethod.POST)
    public ServerResponse getYield(HttpServletRequest request,@RequestBody String yield){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(yield);
        String[] brandType=jsonObject.getString("brandType").split(",");//汽车品牌
        return userService.getYield(brandType);
    }

    /**
     * 添加实验成绩
     * @param request
     * @param experiment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addExperiment",method = RequestMethod.POST)
    public ServerResponse addExperiment(HttpServletRequest request,@RequestBody List<Experiment> experiment){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return userService.addExperiment(experiment,u);
    }


    /**
     * 获取到实验数据
     * @param request
     * @param experiment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getExperiment",method = RequestMethod.POST)
    public ServerResponse<List> getExperiment(HttpServletRequest request,@RequestBody Experiment experiment){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        experiment.setUserId(u.getId());
        return userService.getExperiment(experiment);
    }


    /**
     * 返回实验数据总和
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getExperimentSum",method = RequestMethod.POST)
    public ServerResponse getExperimentSum(HttpServletRequest request,@RequestBody String brand){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(brand);
        String brandType=jsonObject.getString("brandType");
        return userService.getExperimentSum(u,brandType);
    }




    /**
     * 添加季度成绩
     * @param request
     * @param quarter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addQuarter",method = RequestMethod.POST)
    public ServerResponse addQuarter(HttpServletRequest request,@RequestBody Quarter quarter){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        quarter.setUserId(u.getId());
        return userService.addQuarter(quarter);
    }


    /**
     * 获取到季度数据
     * @param request
     * @param quarter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getQuarter",method = RequestMethod.POST)
    public ServerResponse getQuarter(HttpServletRequest request,@RequestBody Quarter quarter){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        quarter.setUserId(u.getId());
        return userService.getQuarter(quarter);
    }


    /**
     * 管理员搜索用户+查看用户
     * @param request
     * @param searchUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchUser",method = RequestMethod.POST)
    public ServerResponse<PageInfo> searchUser(HttpServletRequest request, @RequestBody String searchUser){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(searchUser);
        Integer pageSize=jsonObject.getInt("pageSize");
        Integer pageNum=jsonObject.getInt("pageNum");
        String username= jsonObject.getString("username");
        return userService.searchUser(pageNum,pageSize,username);
    }

    /**
     * 删除用户
     * @param request
     * @param deleteUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    public ServerResponse deleteUser(HttpServletRequest request,@RequestBody String deleteUser){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(deleteUser);
        Integer id=jsonObject.getInt("id");
        return userService.deleteUser(id);
    }

    /**
     * 获取到关系图
     * @param request
     * @param brand
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getLine",method = RequestMethod.POST)
    public ServerResponse getLine(HttpServletRequest request,@RequestBody String brand){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(brand);
        Integer competitionId=jsonObject.getInt("CompetitionId");
        String[] brandType=jsonObject.getString("brandType").toString().split(",");
        return userService.getLine(brandType,competitionId,u);
    }


    /**
     * 获取仿真实验成绩
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getScaleGrade",method = RequestMethod.POST)
    public ServerResponse getScaleGrade(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return userService.getScaleGrade(u);
    }

    /**
     * 上传仿真实验成绩
     * @param request
     * @param commitScaleGrade
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/commitScaleGrade",method = RequestMethod.POST)
    public ServerResponse commitScaleGrade(HttpServletRequest request,@RequestBody String commitScaleGrade){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(commitScaleGrade);
        Integer gradeId=jsonObject.getInt("gradeId");
        Integer scaleScore=jsonObject.getInt("scaleScore");
        String  simulation=jsonObject.getString("simulation");
        Integer aggregateAmount=jsonObject.getInt("aggregateAmount");
        redisUtil.del(u.getId()+"experiment");
        return userService.commitScaleGrade(gradeId,scaleScore,simulation,aggregateAmount);
    }


    /**
     * 导出实验成绩分数
     * @param
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exportExperimentGrade",method = RequestMethod.GET)
    public ServerResponse exportExperimentGrade(String token, HttpServletResponse response){
//        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        List<ExportGrade> exportGrades=new ArrayList<>();
        List<Grade> experimentGrade=userService.exportExperimentGrade(u);
        for (Grade grade : experimentGrade) {
            ExportGrade exportGrade=new ExportGrade();
            exportGrade.setComment(grade.getComment());
            exportGrade.setCompeteObject(grade.getCompeteObject());
            exportGrade.setExperimentalDuration(grade.getExperimentalDuration());
            exportGrade.setGradeDate(grade.getGradeDate());
            exportGrade.setScaleScore(grade.getScaleScore());
            exportGrade.setThemeObject(grade.getThemeObject());
            exportGrade.setUserName(grade.getUserName());
            exportGrade.setTotalScore(grade.getTotalScore());
            exportGrades.add(exportGrade);
        }
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            String fileName = "实验成绩";
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName,"UTF-8")+".xlsx");
            ExcelUtils.writeExcel(out, ExportGrade.class,exportGrades);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 根据源编号来下载数据
     * @param sourceId
     */
    @ResponseBody
    @RequestMapping(value = "/downloadSourceData",method = RequestMethod.GET)
    public void downloadSourceData( Integer sourceId,HttpServletResponse response){
//        String jwt = token.substring(token.lastIndexOf(" ")+1);
//        User u = JwtUtil.unsign(jwt,User.class);
//        if(u == null){
//            return ServerResponse.createByErrorMsg("token无效");
//        }
        String tableName="";
        List<WeiBo>  weiBoList=new ArrayList<>();
        List<WeChat> weChatList=new ArrayList<>();
        List<News> newsList=new ArrayList<>();
        List<Recruitment> recruitmentList=new ArrayList<>();
        List<Patent> patentList=new ArrayList<>();
        List<CompanyWebsite> companyWebsiteList=new ArrayList<>();
        List<CommunityForum> communityForumList=new ArrayList<>();
        List<CorporateAnnualReports> corporateAnnualReportsList=new ArrayList<>();
        List<BusinessInformation> businessInformationList=new ArrayList<>();
        if(sourceId==1){
            weiBoList=userService.getWeiboSourceData();
            tableName="微博数据";
        }else if(sourceId==2){
            weChatList=userService.getWeChatSourceData();
            tableName="微信数据";
        }else if(sourceId==3){
            newsList=userService.getNewsSourceData();
            tableName="新闻数据";
        }else if(sourceId==4){
            recruitmentList=userService.getRecruitmentSourceData();
            tableName="招聘信息数据";
        }else if(sourceId==5){
            patentList=userService.getPatentSourceData();
            tableName="专利信息数据";
        }else if(sourceId==6){
            companyWebsiteList=userService.getCompanyWebsiteSourceData();
            tableName="企业官网信息数据";
        }else if(sourceId==7){
            communityForumList=userService.getCommunityForumSourceData();
            tableName="社区论坛数据";
        }else if(sourceId==8){
            corporateAnnualReportsList=userService.getCorporateAnnualSourceData();
            tableName="企业年报数据";
        }else if(sourceId==9){
            businessInformationList=userService.getBusinessInformationSourceData();
            tableName="工商信息数据";
        }
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
//            String fileName = "tableName";
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(tableName,"UTF-8")+".xlsx");
            if(sourceId==1){
                ExcelUtils.writeExcel(out, WeiBo.class,weiBoList);
            }else if(sourceId==2){
                ExcelUtils.writeExcel(out, WeChat.class,weChatList);
            }else if(sourceId==3){
                ExcelUtils.writeExcel(out, News.class,newsList);
            }else if(sourceId==4){
                ExcelUtils.writeExcel(out, Recruitment.class,recruitmentList);
            }else if(sourceId==5){
                ExcelUtils.writeExcel(out, Patent.class,patentList);
            }else if(sourceId==6){
                ExcelUtils.writeExcel(out, CompanyWebsite.class,companyWebsiteList);
            }else if(sourceId==7){
                ExcelUtils.writeExcel(out, CommunityForum.class,communityForumList);
            }else if(sourceId==8){
                ExcelUtils.writeExcel(out, CorporateAnnualReports.class,corporateAnnualReportsList);
            }else if(sourceId==9){
                ExcelUtils.writeExcel(out, BusinessInformation.class,businessInformationList);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 用户添加问题
     * @param question
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addQuestion",method = RequestMethod.POST)
    public ServerResponse addQuestion(@RequestBody Question question,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return userService.addQuestion(question,u.getId());
    }


    /**
     * 用户添加评论
     * @param answer
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAnswer",method = RequestMethod.POST)
    public ServerResponse addAnswer(@RequestBody Answer answer,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return userService.addAnswer(answer,u.getId());
    }


    /**
     * 用户删除自己的评论(管理员删除任何人的评论)
     * @param data
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAnswer",method = RequestMethod.POST)
    public ServerResponse deleteAnswer(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(data);
        Integer answerId=jsonObject.getInt("answerId");
        return userService.deleteAnswer(answerId,u);
    }


    /**
     * 管理员删除用户提的问题;用户删除自己提的问题
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteQuestion",method = RequestMethod.POST)
    public ServerResponse deleteQuestion(@RequestBody String data){
        JSONObject jsonObject=new JSONObject(data);
        Integer questionId=jsonObject.getInt("questionId");
        return userService.deleteQuestion(questionId);
    }


    /**
     * 获取到评论信息
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAnswer",method = RequestMethod.POST)
    public ServerResponse getAnswer(@RequestBody String data){
        JSONObject jsonObject=new JSONObject(data);
        Integer questionId=jsonObject.getInt("questionId");
        return userService.getAnswer(questionId);
    }


    /**
     * 展示用户填写的问题
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getQuestion",method = RequestMethod.POST)
    public ServerResponse getQuestion(@RequestBody String data){
        JSONObject jsonObject=new JSONObject(data);
        Integer pageNum=jsonObject.getInt("pageNum");
        Integer pageSize=jsonObject.getInt("pageSize");
        String username=jsonObject.getString("username");
        String school=jsonObject.getString("school");
        String question=jsonObject.getString("question");
        return userService.getQuestion(pageNum,pageSize,username,school,question);
    }

}
